package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseAdapter;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.FunctionInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class FunctionAdapter extends BaseAdapter {
    private List<FunctionInfo.DataBean> modelList;
    public FunctionAdapter(Context context) {
        super(context);
        modelList = new ArrayList<>();
    }
    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<FunctionInfo.DataBean> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    public void addAllData(List<FunctionInfo.DataBean> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_function,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        final FunctionInfo.DataBean itemModel = modelList.get(i);
        storeHolder.function_name_text.setText(itemModel.getTitle());
        storeHolder.function_time_text.setText(itemModel.getCreate_time());
        storeHolder.content_text.setText(itemModel.getUpdates());
        storeHolder.top_content_parent.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (storeHolder.content_text.getVisibility() == View.VISIBLE){
                    storeHolder.content_text.setVisibility(View.GONE);
                    storeHolder.arrow_icon.setImageResource(R.mipmap.bottom_arrow);
                }else {
                    storeHolder.content_text.setVisibility(View.VISIBLE);
                    storeHolder.arrow_icon.setImageResource(R.mipmap.top_arrow);
                }
                notifyDataSetChanged();
            }
        });
        return view;
    }
    /**
     *
     */
    class Holder{
        private LinearLayout top_content_parent;
        private TextView function_name_text;
        private TextView function_time_text;
        private TextView content_text;
        private ImageView arrow_icon;
        void initView(View view) {
            top_content_parent = (LinearLayout) view.findViewById(R.id.top_content_parent);
            function_name_text = (TextView) view.findViewById(R.id.function_name_text);
            function_time_text = (TextView) view.findViewById(R.id.function_time_text);
            content_text = (TextView) view.findViewById(R.id.content_text);
            arrow_icon = (ImageView) view.findViewById(R.id.arrow_icon);
        }

    }
}
